//
//  testmain.cpp
//  DataPlatform
//
//  Created by Liu Dafan on 12-2-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#include <inttypes.h>
#include "base.h"
#include "datastream.h"
#include "crc.h"
#include "zlib.h"
#include <string>

using namespace std;

extern void dir_operation_test();
extern void file_operation_test();
extern void test_init();
extern void test_uninit();

void Log_test()
{
    char *largestr = new char[8162];
    memset(largestr, 'a', 8162);
    DINFO("%s", largestr);
}

void CRC_test()
{
    const char* tmp = "hello world";
    
    CRC16 crc16 = boost_crc16(tmp, strlen(tmp));
    printf("Data CRC16: %u\n", crc16);
    
    CRC32 crc32_0 = boost_crc32(tmp, strlen(tmp));
    printf("Data CRC32: %u\n", crc32_0);
    
    CRC32 crc32_1 = crc32(crc32(0L, Z_NULL, 0), (const Byte *)tmp, strlen(tmp));
    printf("Data zlib CRC32: %u\n", crc32_1);
    
    CRC64 crc64 = boost_crc64(tmp, strlen(tmp));
    printf("Data CRC64: %llu\n", crc64);
    
    printf("CRC calculation test done\n");
}

void Stream_test()
{
    DataStream stream;
    string strdata = "hello world";
    uint32_t intdata = 1000;
    uint64_t longdata = 1000;
    stream.Serialize(strdata);
    stream.Serialize(intdata);
    stream.Serialize(longdata);

    string retstring;
    uint32_t retint;
    uint64_t retlong;
    stream.reset();
    stream.UnSerialize(retstring);
    stream.UnSerialize(retint);
    stream.UnSerialize(retlong);
    if (retstring != strdata)
    {
        printf("String test failed, org: %s, new: %s\n",
               strdata.c_str(), retstring.c_str());
    }

    if (retint != intdata)
    {
        printf("int32_t test failed\n");
    }

    if (retlong != longdata)
    {
        printf("int64_t test failed\n");
    }

    printf("Stream test done\n");
}

int main(int argc, char** argv)
{
    test_init();
    logfile_create("test.log", 4);
    Stream_test();
    CRC_test();
    Log_test();
    dir_operation_test();
    file_operation_test();
    test_uninit();
}
